package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.classes;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.ClassesListActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ClassesListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class ClassesListActivity extends BaseActivity {
    private ClassesListActivityCustom dataBinding;
    private SlimAdapterEx<ClassesListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_classes_list);
        initTopTitleBar(View.VISIBLE, "班次列表");
        initTopRightText("添加", v -> {
            startActivityForResult(AddClassesActivity.class, null, Config.ADD_CLASSES);
        });
    }

    @Override
    public void initData() {
        Random random = new Random(new Date().getTime());
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_CLASSES)
                    .params("userGuid", user.getGuid())
                    .params("shiftId", slimAdapterEx.getDataItem(pos).getId())
                    .params("companyId", slimAdapterEx.getDataItem(pos).getCompanyId())
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                menuBridge.closeMenu();
                                slimAdapterEx.getData().remove(pos);
                                slimAdapterEx.notifyItemRemoved(pos);
                            }
                        }
                    });
        });
        dataBinding.recyclerView.setSwipeItemClickListener((view, pos) -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("bean", slimAdapterEx.getDataItem(pos));
            startActivityForResult(UpdateClassesActivity.class, bundle, pos);
        });
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_43, new SlimInjector<ClassesListResultBean>() {
            @Override
            public void onInject(ClassesListResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_title, data.getShiftName())
                        .text(R.id.tv_time_1, data.getStartTime1().substring(0, 5) + "~" + data.getEndTime1().substring(0, 5))
                        .text(R.id.tv_time_2, data.getStartTime2().substring(0, 5) + "~" + data.getEndTime2().substring(0, 5))
                        .text(R.id.tv_time_3, data.getStartTime3().substring(0, 5) + "~" + data.getEndTime3().substring(0, 5))
                        .text(R.id.tv_time_4, data.getStartTime4().substring(0, 5) + "~" + data.getEndTime4().substring(0, 5))
                        .visibility(R.id.cardView_2, data.getStartTime2().equals("00:00:00") && data.getEndTime2().equals("00:00:00") ? View.GONE : View.VISIBLE)
                        .visibility(R.id.cardView_3, data.getStartTime3().equals("00:00:00") && data.getEndTime3().equals("00:00:00") ? View.GONE : View.VISIBLE)
                        .visibility(R.id.cardView_4, data.getStartTime4().equals("00:00:00") && data.getEndTime4().equals("00:00:00") ? View.GONE : View.VISIBLE)
                        .with(R.id.cardView_1, v ->
                                ((CardView) v).setCardBackgroundColor(
                                        Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255))))
                        .with(R.id.cardView_2, v ->
                                ((CardView) v).setCardBackgroundColor(
                                        Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255))))
                        .with(R.id.cardView_3, v ->
                                ((CardView) v).setCardBackgroundColor(
                                        Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255))))
                        .with(R.id.cardView_4, v ->
                                ((CardView) v).setCardBackgroundColor(
                                        Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255))));
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<List<ClassesListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.CLASSES_LIST)
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<List<ClassesListResultBean>>>(ClassesListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<ClassesListResultBean>>> response) {
                        HttpResult<List<ClassesListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.ADD_CLASSES) {
                setView();
            } else {
                slimAdapterEx.getData().remove(requestCode);
                slimAdapterEx.getData().add(requestCode, (ClassesListResultBean) data.getSerializableExtra("bean"));
                slimAdapterEx.notifyItemChanged(requestCode);
            }
        }
    }
}
